﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mailServer = LightSwitchApplication.local.mail;
namespace LightSwitchApplication.UserCode
{
    public class ExchangeHelper
    {
        /// <summary>
        /// This method is used to send email with attachment
        /// </summary>
        /// <param name="from">from address</param>
        /// <param name="to">to addess</param>
        /// <param name="subject">subject of the mail</param>
        /// <param name="body">body</param>
        public static void SendEmailExchangeWebService(string from, string to, string subject, string body) //, byte[] attachmentAsBytes, string attachmentName, string cc, s
        {
            try
            {
                using (ExchangeServiceBinding ewsBinding = new ExchangeServiceBinding())
                {

                    //Set the exchange web service properties URL and credential 
                    ewsBinding.Url = "https://intranet.local/ews/exchange.asmx";
                    ewsBinding.UseDefaultCredentials = true; // ewsBinding.Credentials = new NetworkCredential("userName", "Password", "Domain");

                    // CreateItemType and set its relevant properties/values        
                    CreateItemType request = new CreateItemType();               
                    // Select Message Disposition specified        
                    request.MessageDisposition = MessageDispositionType.SendOnly;        
                    request.MessageDispositionSpecified = true;             
                    // Create MessageType and set its relevant properties/values        
                    MessageType message = new MessageType();         
                    // Enter value for subject        
                    message.Subject = subject;        
                    message.Body = new BodyType();        
                    message.Body.BodyType1 = BodyTypeType.HTML;        
                    // Enter value for body        
                    message.Body.Value = body;        
                    message.ToRecipients = new EmailAddressType[1];        
                    message.ToRecipients[0] = new EmailAddressType();              
                    // Enter ToEmailAddress        
                    message.ToRecipients[0].EmailAddress = to;        
                    // Select Routing Type        
                    message.ToRecipients[0].RoutingType = "SMTP";        
                    request.Items = new NonEmptyArrayOfAllItemsType();        
                    request.Items.Items = new ItemType[1];       
                    request.Items.Items[0] = message;        
                    try        
                    {
                        CreateItemResponseType response = ewsBinding.CreateItem(request);            
                        // Determine whether the request was a success.            
                        if (response.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)            
                        {
                            throw new Exception(response.ResponseMessages.Items[0].MessageText);            
                        }
                    }          
                    catch(Exception e)            
                    {
                        throw e;                
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
                //Console.WriteLine("Exception in sending mail " + ex.ToString());
            }
        }
         
    }
}